package dd.control;

import java.util.ArrayList;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import xml.Message;
import dd.model.Edge;
import dd.model.Event;
import dd.model.Model;
import dd.view.addEdgeFrame;

public class AddEdgeResponseController {
	/**
	 * this controller called when a edge response come, add an edge to the event. repaint the screen
	 */
	public void process(Message response) {
		Model model = Model.getInstance();
		Event e = model.getEvent();
		ArrayList<Edge> ledge = e.getedgelist();

		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();
		String geteventid = map.getNamedItem("id").getNodeValue();
		System.out.println("Event ID: " + geteventid);
		String leftt = map.getNamedItem("left").getNodeValue();
		int left = Integer.parseInt(leftt);
		String rightt = map.getNamedItem("right").getNodeValue();
		int right = Integer.parseInt(rightt);
		String heightt = map.getNamedItem("height").getNodeValue();
		int height = Integer.parseInt(heightt);
		System.out.println("received: " + leftt + " " + rightt + " " + heightt);
		// if this is the update of my event, update the addChoiceFrame
		if (geteventid.equals(e.getEventID())) {
			Edge edge = new Edge();
			edge.setEdge(left, right, height);
			ledge.add(edge);
			// draw all the edge, in should change this in the future.
			e.setedgelist(ledge);
			addEdgeFrame aeff = model.getaef();
			aeff.getDrawingCanvas().repaint();
			aeff.repaint();
			aeff.getDrawingCanvas().calcResult(ledge);
			// all the edge have been added
			// if(ledge.size()==(e.getNumberEdges()*e.getNumberLines())){
			System.out.println("@@@@@@@@@@@@@@@"+e.getleftEdges());
			
			// if (e.getleftEdges() <= 0) {
			// // ResultScreen rs = new ResultScreen();
			// // rs.setVisible(true);
			// // aeff.dispose();
			// model.getaef().getLblOpenModePlease()
			// .setText("Your turn is over, please wait for the admin's operation");
			// }
		}
	}
}
